package com.nfit.shopweb.web.api;

import com.github.pagehelper.PageInfo;
import com.nfit.shopmodel.dto.product.ProductDto;
import com.nfit.shopmodel.entity.product.Product;
import com.nfit.shopmodel.vo.common.Result;
import com.nfit.shopmodel.vo.common.ResultCodeEnum;
import com.nfit.shopweb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName:ProductController
 * Package:com.nfit.shopweb.web.api
 * Description
 *
 * @Author L
 * @Create 2024/12/17 10:51
 */
@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    public Result page(@PathVariable("pageNum") Integer pageNum,
                       @PathVariable("pageSize") Integer pageSize,
                       @RequestBody(required = false) ProductDto productDto){
       PageInfo<Product> pageInfo = productService.page(pageNum,pageSize,productDto);
       return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据商品id查询商品信息
     * @param id 商品id
     */
    @GetMapping("findById/{id}")
    public Result findById(@PathVariable("id") Long id){
       Product product = productService.findById(id);
       return Result.build(product,ResultCodeEnum.SUCCESS);
    }

    /**
     * 修改商品
     * @param product 商品实体
     */
    @PutMapping("/update")
    public Result update(@RequestBody Product product){
        productService.update(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/save")
    public Result save(@RequestBody Product product){
        productService.save(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        productService.delete(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 审批商品
     * @param productId 商品id
     * @param auditStatus 审批状态，前端传 1 表示通过 -1 表示驳回
     */
    @GetMapping("/updateAuditStatus/{productId}/{auditStatus}")
    public Result updateAuditStatus(@PathVariable("productId") Long productId,
                                    @PathVariable("auditStatus") Integer auditStatus){
        productService.updateAuditStatus(productId,auditStatus);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 上下架商品
     * @param productId 商品id
     * @param status 上下架状态： 前端传 1 表示上架 -1 表示下架
     */
    @GetMapping("/updateStatus/{productId}/{status}")
    public Result updateStatus(@PathVariable("productId") Long productId,
                               @PathVariable("status") Integer status){
        productService.updateStatus(productId,status);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

}
